home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / gas / config / tc-sh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  42.4 KB  |  1,888 lines

  1. /* tc-sh.c -- Assemble code for the Hitachi Super-H
  2.  
  3.    Copyright (C) 1993, 94, 95, 1996 Free Software Foundation.
  4.  
  5.    This file is part of GAS, the GNU Assembler.
  6.  
  7.    GAS is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    GAS is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with GAS; see the file COPYING.  If not, write to
  19.    the Free Software Foundation, 59 Temple Place - Suite 330,
  20.    Boston, MA 02111-1307, USA.  */
  21.  
  22. /*
  23.    Written By Steve Chamberlain
  24.    sac@cygnus.com
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include "as.h"
  29. #include "bfd.h"
  30. #include "subsegs.h"
  31. #define DEFINE_TABLE
  32. #include "opcodes/sh-opc.h"
  33. #include <ctype.h>
  34. const char comment_chars[] = "!";
  35. const char line_separator_chars[] = ";";
  36. const char line_comment_chars[] = "!#";
  37.  
  38. static void s_uses PARAMS ((int));
  39.  
  40. /* This table describes all the machine specific pseudo-ops the assembler
  41.    has to support.  The fields are:
  42.    pseudo-op name without dot
  43.    function to call to execute this pseudo-op
  44.    Integer arg to pass to the function
  45.  */
  46.  
  47. void cons ();
  48. void s_align_bytes ();
  49.  
  50. int shl = 0;
  51.  
  52. static void
  53. little (ignore)
  54.      int ignore;
  55. {
  56.   shl = 1;
  57.   target_big_endian = 0;
  58. }
  59.  
  60. const pseudo_typeS md_pseudo_table[] =
  61. {
  62.   {"int", cons, 4},
  63.   {"word", cons, 2},
  64.   {"form", listing_psize, 0},
  65.   {"little", little, 0},
  66.   {"heading", listing_title, 0},
  67.   {"import", s_ignore, 0},
  68.   {"page", listing_eject, 0},
  69.   {"program", s_ignore, 0},
  70.   {"uses", s_uses, 0},
  71.   {0, 0, 0}
  72. };
  73.  
  74. /*int md_reloc_size; */
  75.  
  76. int sh_relax;        /* set if -relax seen */
  77.  
  78. const char EXP_CHARS[] = "eE";
  79.  
  80. /* Chars that mean this number is a floating point constant */
  81. /* As in 0f12.456 */
  82. /* or    0d1.2345e12 */
  83. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  84.  
  85. #define C(a,b) ENCODE_RELAX(a,b)
  86.  
  87. #define JREG 14            /* Register used as a temp when relaxing */
  88. #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
  89. #define GET_WHAT(x) ((x>>4))
  90.  
  91. /* These are the two types of relaxable instrction */
  92. #define COND_JUMP 1
  93. #define UNCOND_JUMP  2
  94.  
  95. #define UNDEF_DISP 0
  96. #define COND8  1
  97. #define COND12 2
  98. #define COND32 3
  99. #define UNCOND12 1
  100. #define UNCOND32 2
  101. #define UNDEF_WORD_DISP 4
  102. #define END 5
  103.  
  104. #define UNCOND12 1
  105. #define UNCOND32 2
  106.  
  107. /* Branch displacements are from the address of the branch plus
  108.    four, thus all minimum and maximum values have 4 added to them.  */
  109. #define COND8_F 258
  110. #define COND8_M -252
  111. #define COND8_LENGTH 2
  112.  
  113. /* There is one extra instruction before the branch, so we must add
  114.    two more bytes to account for it.  */
  115. #define COND12_F 4100
  116. #define COND12_M -4090
  117. #define COND12_LENGTH 6
  118.  
  119. /* ??? The minimum and maximum values are wrong, but this does not matter
  120.    since this relocation type is not supported yet.  */
  121. #define COND32_F (1<<30)
  122. #define COND32_M -(1<<30)
  123. #define COND32_LENGTH 14
  124.  
  125. #define UNCOND12_F 4098
  126. #define UNCOND12_M -4092
  127. #define UNCOND12_LENGTH 2
  128.  
  129. /* ??? The minimum and maximum values are wrong, but this does not matter
  130.    since this relocation type is not supported yet.  */
  131. #define UNCOND32_F (1<<30)
  132. #define UNCOND32_M -(1<<30)
  133. #define UNCOND32_LENGTH 14
  134.  
  135. const relax_typeS md_relax_table[C (END, 0)] = {
  136.   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  137.   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  138.  
  139.   { 0 },
  140.   /* C (COND_JUMP, COND8) */
  141.   { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
  142.   /* C (COND_JUMP, COND12) */
  143.   { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
  144.   /* C (COND_JUMP, COND32) */
  145.   { COND32_F, COND32_M, COND32_LENGTH, 0, },
  146.   { 0 }, { 0 }, { 0 }, { 0 },
  147.   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  148.  
  149.   { 0 },
  150.   /* C (UNCOND_JUMP, UNCOND12) */
  151.   { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
  152.   /* C (UNCOND_JUMP, UNCOND32) */
  153.   { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
  154.   { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  155.   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  156. };
  157.  
  158. static struct hash_control *opcode_hash_control;    /* Opcode mnemonics */
  159.  
  160. /*
  161.    This function is called once, at assembler startup time.  This should
  162.    set up all the tables, etc that the MD part of the assembler needs
  163.  */
  164.  
  165. void
  166. md_begin ()
  167. {
  168.   sh_opcode_info *opcode;
  169.   char *prev_name = "";
  170.  
  171.   if (! shl)
  172.     target_big_endian = 1;
  173.  
  174.   opcode_hash_control = hash_new ();
  175.  
  176.   /* Insert unique names into hash table */
  177.   for (opcode = sh_table; opcode->name; opcode++)
  178.     {
  179.       if (strcmp (prev_name, opcode->name))
  180.     {
  181.       prev_name = opcode->name;
  182.       hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
  183.     }
  184.       else
  185.     {
  186.       /* Make all the opcodes with the same name point to the same
  187.          string */
  188.       opcode->name = prev_name;
  189.     }
  190.     }
  191. }
  192.  
  193. static int reg_m;
  194. static int reg_n;
  195. static int reg_b;
  196.  
  197. static expressionS immediate;    /* absolute expression */
  198.  
  199. typedef struct
  200.   {
  201.     sh_arg_type type;
  202.     int reg;
  203.   }
  204.  
  205. sh_operand_info;
  206.  
  207. /* try and parse a reg name, returns number of chars consumed */
  208. static int
  209. parse_reg (src, mode, reg)
  210.      char *src;
  211.      int *mode;
  212.      int *reg;
  213. {
  214.   /* We use !isalnum for the next character after the register name, to
  215.      make sure that we won't accidentally recognize a symbol name such as
  216.      'sram' as being a reference to the register 'sr'.  */
  217.  
  218.   if (src[0] == 'r')
  219.     {
  220.       if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0
  221.       && ! isalnum (src[7]))
  222.     {
  223.       *mode = A_REG_B;
  224.       *reg  = (src[1] - '0');
  225.       return 7;
  226.     }
  227.     }
  228.  
  229.   if (src[0] == 'r')
  230.     {
  231.       if (src[1] == '1')
  232.     {
  233.       if (src[2] >= '0' && src[2] <= '5' && ! isalnum (src[3]))
  234.         {
  235.           *mode = A_REG_N;
  236.           *reg = 10 + src[2] - '0';
  237.           return 3;
  238.         }
  239.     }
  240.       if (src[1] >= '0' && src[1] <= '9' && ! isalnum (src[2]))
  241.     {
  242.       *mode = A_REG_N;
  243.       *reg = (src[1] - '0');
  244.       return 2;
  245.     }
  246.     }
  247.  
  248.   if (src[0] == 's' && src[1] == 's' && src[2] == 'r' && ! isalnum (src[3]))
  249.     {
  250.       *mode = A_SSR;
  251.       return 3;
  252.     }
  253.  
  254.   if (src[0] == 's' && src[1] == 'p' && src[2] == 'c' && ! isalnum (src[3]))
  255.     {
  256.       *mode = A_SPC;
  257.       return 3;
  258.     }
  259.  
  260.   if (src[0] == 's' && src[1] == 'r' && ! isalnum (src[2]))
  261.     {
  262.       *mode = A_SR;
  263.       return 2;
  264.     }
  265.  
  266.   if (src[0] == 's' && src[1] == 'p' && ! isalnum (src[2]))
  267.     {
  268.       *mode = A_REG_N;
  269.       *reg = 15;
  270.       return 2;
  271.     }
  272.  
  273.   if (src[0] == 'p' && src[1] == 'r' && ! isalnum (src[2]))
  274.     {
  275.       *mode = A_PR;
  276.       return 2;
  277.     }
  278.   if (src[0] == 'p' && src[1] == 'c' && ! isalnum (src[2]))
  279.     {
  280.       *mode = A_DISP_PC;
  281.       return 2;
  282.     }
  283.   if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
  284.     {
  285.       *mode = A_GBR;
  286.       return 3;
  287.     }
  288.   if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
  289.     {
  290.       *mode = A_VBR;
  291.       return 3;
  292.     }
  293.  
  294.   if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c' && ! isalnum (src[4]))
  295.     {
  296.       if (src[3] == 'l')
  297.     {
  298.       *mode = A_MACL;
  299.       return 4;
  300.     }
  301.       if (src[3] == 'h')
  302.     {
  303.       *mode = A_MACH;
  304.       return 4;
  305.     }
  306.     }
  307.   if (src[0] == 'f' && src[1] == 'r')
  308.     {
  309.       if (src[2] == '1')
  310.     {
  311.       if (src[3] >= '0' && src[3] <= '5' && ! isalnum (src[4]))
  312.         {
  313.           *mode = F_REG_N;
  314.           *reg = 10 + src[3] - '0';
  315.           return 4;
  316.         }
  317.     }
  318.       if (src[2] >= '0' && src[2] <= '9' && ! isalnum (src[3]))
  319.     {
  320.       *mode = F_REG_N;
  321.       *reg = (src[2] - '0');
  322.       return 3;
  323.     }
  324.     }
  325.   if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
  326.       && ! isalnum (src[4]))
  327.     {
  328.       *mode = FPUL_N;
  329.       return 4;
  330.     }
  331.  
  332.   if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
  333.       && src[4] == 'r' && ! isalnum (src[5]))
  334.     {
  335.       *mode = FPSCR_N;
  336.       return 5;
  337.     }
  338.  
  339.   return 0;
  340. }
  341.  
  342. static symbolS *dot()
  343. {
  344.   const char *fake;
  345.  
  346.   /* JF: '.' is pseudo symbol with value of current location
  347.      in current segment.  */
  348.   fake = FAKE_LABEL_NAME;
  349.   return  symbol_new (fake,
  350.               now_seg,
  351.               (valueT) frag_now_fix (),
  352.               frag_now);
  353.  
  354. }
  355.  
  356.  
  357. static
  358. char *
  359. parse_exp (s)
  360.      char *s;
  361. {
  362.   char *save;
  363.   char *new;
  364.  
  365.   save = input_line_pointer;
  366.   input_line_pointer = s;
  367.   expression (&immediate);
  368.   if (immediate.X_op == O_absent)
  369.     as_bad ("missing operand");
  370.   new = input_line_pointer;
  371.   input_line_pointer = save;
  372.   return new;
  373. }
  374.  
  375.  
  376. /* The many forms of operand:
  377.  
  378.    Rn                   Register direct
  379.    @Rn                  Register indirect
  380.    @Rn+                 Autoincrement
  381.    @-Rn                 Autodecrement
  382.    @(disp:4,Rn)
  383.    @(disp:8,GBR)
  384.    @(disp:8,PC)
  385.  
  386.    @(R0,Rn)
  387.    @(R0,GBR)
  388.  
  389.    disp:8
  390.    disp:12
  391.    #imm8
  392.    pr, gbr, vbr, macl, mach
  393.  
  394.  */
  395.  
  396. static
  397. char *
  398. parse_at (src, op)
  399.      char *src;
  400.      sh_operand_info *op;
  401. {
  402.   int len;
  403.   int mode;
  404.   src++;
  405.   if (src[0] == '-')
  406.     {
  407.       /* Must be predecrement */
  408.       src++;
  409.  
  410.       len = parse_reg (src, &mode, &(op->reg));
  411.       if (mode != A_REG_N)
  412.     as_bad ("illegal register after @-");
  413.  
  414.       op->type = A_DEC_N;
  415.       src += len;
  416.     }
  417.   else if (src[0] == '(')
  418.     {
  419.       /* Could be @(disp, rn), @(disp, gbr), @(disp, pc),  @(r0, gbr) or
  420.          @(r0, rn) */
  421.       src++;
  422.       len = parse_reg (src, &mode, &(op->reg));
  423.       if (len && mode == A_REG_N)
  424.     {
  425.       src += len;
  426.       if (op->reg != 0)
  427.         {
  428.           as_bad ("must be @(r0,...)");
  429.         }
  430.       if (src[0] == ',')
  431.         src++;
  432.       /* Now can be rn or gbr */
  433.       len = parse_reg (src, &mode, &(op->reg));
  434.       if (mode == A_GBR)
  435.         {
  436.           op->type = A_R0_GBR;
  437.         }
  438.       else if (mode == A_REG_N)
  439.         {
  440.           op->type = A_IND_R0_REG_N;
  441.         }
  442.       else
  443.         {
  444.           as_bad ("syntax error in @(r0,...)");
  445.         }
  446.     }
  447.       else
  448.     {
  449.       /* Must be an @(disp,.. thing) */
  450.       src = parse_exp (src);
  451.       if (src[0] == ',')
  452.         src++;
  453.       /* Now can be rn, gbr or pc */
  454.       len = parse_reg (src, &mode, &op->reg);
  455.       if (len)
  456.         {
  457.           if (mode == A_REG_N)
  458.         {
  459.           op->type = A_DISP_REG_N;
  460.         }
  461.           else if (mode == A_GBR)
  462.         {
  463.           op->type = A_DISP_GBR;
  464.         }
  465.           else if (mode == A_DISP_PC)
  466.         {
  467.           /* Turn a plain @(4,pc) into @(.+4,pc) */
  468.           if (immediate.X_op == O_constant) { 
  469.             immediate.X_add_symbol = dot();
  470.             immediate.X_op = O_symbol;
  471.           }
  472.           op->type = A_DISP_PC;
  473.         }
  474.           else
  475.         {
  476.           as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
  477.         }
  478.         }
  479.       else
  480.         {
  481.           as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
  482.         }
  483.     }
  484.       src += len;
  485.       if (src[0] != ')')
  486.     as_bad ("expecting )");
  487.       else
  488.     src++;
  489.     }
  490.   else
  491.     {
  492.       src += parse_reg (src, &mode, &(op->reg));
  493.       if (mode != A_REG_N)
  494.     {
  495.       as_bad ("illegal register after @");
  496.     }
  497.       if (src[0] == '+')
  498.     {
  499.       op->type = A_INC_N;
  500.       src++;
  501.     }
  502.       else
  503.     {
  504.       op->type = A_IND_N;
  505.     }
  506.     }
  507.   return src;
  508. }
  509.  
  510. static void
  511. get_operand (ptr, op)
  512.      char **ptr;
  513.      sh_operand_info *op;
  514. {
  515.   char *src = *ptr;
  516.   int mode = -1;
  517.   unsigned int len;
  518.  
  519.   if (src[0] == '#')
  520.     {
  521.       src++;
  522.       *ptr = parse_exp (src);
  523.       op->type = A_IMM;
  524.       return;
  525.     }
  526.  
  527.   else if (src[0] == '@')
  528.     {
  529.       *ptr = parse_at (src, op);
  530.       return;
  531.     }
  532.   len = parse_reg (src, &mode, &(op->reg));
  533.   if (len)
  534.     {
  535.       *ptr = src + len;
  536.       op->type = mode;
  537.       return;
  538.     }
  539.   else
  540.     {
  541.       /* Not a reg, the only thing left is a displacement */
  542.       *ptr = parse_exp (src);
  543.       op->type = A_DISP_PC;
  544.       return;
  545.     }
  546. }
  547.  
  548. static
  549. char *
  550. get_operands (info, args, operand)
  551.      sh_opcode_info *info;
  552.      char *args;
  553.      sh_operand_info *operand;
  554.  
  555. {
  556.   char *ptr = args;
  557.   if (info->arg[0])
  558.     {
  559.       ptr++;
  560.  
  561.       get_operand (&ptr, operand + 0);
  562.       if (info->arg[1])
  563.     {
  564.       if (*ptr == ',')
  565.         {
  566.           ptr++;
  567.         }
  568.       get_operand (&ptr, operand + 1);
  569.       if (info->arg[2])
  570.         {
  571.           if (*ptr == ',')
  572.         {
  573.           ptr++;
  574.         }
  575.           get_operand (&ptr, operand + 2);
  576.         }
  577.       else
  578.         {
  579.           operand[2].type = 0;
  580.         }
  581.     }
  582.       else
  583.     {
  584.       operand[1].type = 0;
  585.       operand[2].type = 0;
  586.     }
  587.     }
  588.   else
  589.     {
  590.       operand[0].type = 0;
  591.       operand[1].type = 0;
  592.       operand[2].type = 0;
  593.     }
  594.   return ptr;
  595. }
  596.  
  597. /* Passed a pointer to a list of opcodes which use different
  598.    addressing modes, return the opcode which matches the opcodes
  599.    provided
  600.  */
  601.  
  602. static
  603. sh_opcode_info *
  604. get_specific (opcode, operands)
  605.      sh_opcode_info *opcode;
  606.      sh_operand_info *operands;
  607. {
  608.   sh_opcode_info *this_try = opcode;
  609.   char *name = opcode->name;
  610.   int n = 0;
  611.   while (opcode->name)
  612.     {
  613.       this_try = opcode++;
  614.       if (this_try->name != name)
  615.     {
  616.       /* We've looked so far down the table that we've run out of
  617.          opcodes with the same name */
  618.       return 0;
  619.     }
  620.       /* look at both operands needed by the opcodes and provided by
  621.          the user - since an arg test will often fail on the same arg
  622.          again and again, we'll try and test the last failing arg the
  623.          first on each opcode try */
  624.  
  625.       for (n = 0; this_try->arg[n]; n++)
  626.     {
  627.       sh_operand_info *user = operands + n;
  628.       sh_arg_type arg = this_try->arg[n];
  629.       switch (arg)
  630.         {
  631.         case A_IMM:
  632.         case A_BDISP12:
  633.         case A_BDISP8:
  634.         case A_DISP_GBR:
  635.         case A_DISP_PC:
  636.         case A_MACH:
  637.         case A_PR:
  638.         case A_MACL:
  639.           if (user->type != arg)
  640.         goto fail;
  641.           break;
  642.         case A_R0:
  643.           /* opcode needs r0 */
  644.           if (user->type != A_REG_N || user->reg != 0)
  645.         goto fail;
  646.           break;
  647.         case A_R0_GBR:
  648.           if (user->type != A_R0_GBR || user->reg != 0)
  649.         goto fail;
  650.           break;
  651.         case F_FR0:
  652.           if (user->type != F_REG_N || user->reg != 0)
  653.         goto fail;
  654.           break;
  655.  
  656.         case A_REG_N:
  657.         case A_INC_N:
  658.         case A_DEC_N:
  659.         case A_IND_N:
  660.         case A_IND_R0_REG_N:
  661.         case A_DISP_REG_N:
  662.         case F_REG_N:
  663.         case FPUL_N:
  664.         case FPSCR_N:
  665.           /* Opcode needs rn */
  666.           if (user->type != arg)
  667.         goto fail;
  668.           reg_n = user->reg;
  669.           break;
  670.         case A_GBR:
  671.         case A_SR:
  672.         case A_VBR:
  673.         case A_SSR:
  674.         case A_SPC:
  675.           if (user->type != arg)
  676.         goto fail;
  677.           break;
  678.  
  679.             case A_REG_B:
  680.           if (user->type != arg)
  681.         goto fail;
  682.           reg_b = user->reg;
  683.           break;
  684.  
  685.         case A_REG_M:
  686.         case A_INC_M:
  687.         case A_DEC_M:
  688.         case A_IND_M:
  689.         case A_IND_R0_REG_M:
  690.         case A_DISP_REG_M:
  691.           /* Opcode needs rn */
  692.           if (user->type != arg - A_REG_M + A_REG_N)
  693.         goto fail;
  694.           reg_m = user->reg;
  695.           break;
  696.  
  697.         case F_REG_M:
  698.         case FPUL_M:
  699.         case FPSCR_M:
  700.           /* Opcode needs rn */
  701.           if (user->type != arg - F_REG_M + F_REG_N)
  702.         goto fail;
  703.           reg_m = user->reg;
  704.           break;
  705.     
  706.         default:
  707.           printf ("unhandled %d\n", arg);
  708.           goto fail;
  709.         }
  710.     }
  711.       return this_try;
  712.     fail:;
  713.     }
  714.  
  715.   return 0;
  716. }
  717.  
  718. int
  719. check (operand, low, high)
  720.      expressionS *operand;
  721.      int low;
  722.      int high;
  723. {
  724.   if (operand->X_op != O_constant
  725.       || operand->X_add_number < low
  726.       || operand->X_add_number > high)
  727.     {
  728.       as_bad ("operand must be absolute in range %d..%d", low, high);
  729.     }
  730.   return operand->X_add_number;
  731. }
  732.  
  733.  
  734. static void
  735. insert (where, how, pcrel)
  736.      char *where;
  737.      int how;
  738.      int pcrel;
  739. {
  740.   fix_new_exp (frag_now,
  741.            where - frag_now->fr_literal,
  742.            2,
  743.            &immediate,
  744.            pcrel,
  745.            how);
  746. }
  747.  
  748. static void
  749. build_relax (opcode)
  750.      sh_opcode_info *opcode;
  751. {
  752.   int high_byte = target_big_endian ? 0 : 1;
  753.   char *p;
  754.  
  755.   if (opcode->arg[0] == A_BDISP8)
  756.     {
  757.       p = frag_var (rs_machine_dependent,
  758.             md_relax_table[C (COND_JUMP, COND32)].rlx_length,
  759.             md_relax_table[C (COND_JUMP, COND8)].rlx_length,
  760.             C (COND_JUMP, 0),
  761.             immediate.X_add_symbol,
  762.             immediate.X_add_number,
  763.             0);
  764.       p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
  765.     }
  766.   else if (opcode->arg[0] == A_BDISP12)
  767.     {
  768.       p = frag_var (rs_machine_dependent,
  769.             md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
  770.             md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
  771.             C (UNCOND_JUMP, 0),
  772.             immediate.X_add_symbol,
  773.             immediate.X_add_number,
  774.             0);
  775.       p[high_byte] = (opcode->nibbles[0] << 4);
  776.     }
  777.  
  778. }
  779.  
  780. /* Now we know what sort of opcodes it is, lets build the bytes -
  781.  */
  782. static void
  783. build_Mytes (opcode, operand)
  784.      sh_opcode_info *opcode;
  785.      sh_operand_info *operand;
  786.  
  787. {
  788.   int index;
  789.   char nbuf[4];
  790.   char *output = frag_more (2);
  791.   int low_byte = target_big_endian ? 1 : 0;
  792.   nbuf[0] = 0;
  793.   nbuf[1] = 0;
  794.   nbuf[2] = 0;
  795.   nbuf[3] = 0;
  796.  
  797.   for (index = 0; index < 4; index++)
  798.     {
  799.       sh_nibble_type i = opcode->nibbles[index];
  800.       if (i < 16)
  801.     {
  802.       nbuf[index] = i;
  803.     }
  804.       else
  805.     {
  806.       switch (i)
  807.         {
  808.         case REG_N:
  809.           nbuf[index] = reg_n;
  810.           break;
  811.         case REG_M:
  812.           nbuf[index] = reg_m;
  813.           break;
  814.             case REG_B:
  815.           nbuf[index] = reg_b | 0x08;
  816.           break;
  817.         case DISP_4:
  818.           insert (output + low_byte, R_SH_IMM4, 0);
  819.           break;
  820.         case IMM_4BY4:
  821.           insert (output + low_byte, R_SH_IMM4BY4, 0);
  822.           break;
  823.         case IMM_4BY2:
  824.           insert (output + low_byte, R_SH_IMM4BY2, 0);
  825.           break;
  826.         case IMM_4:
  827.           insert (output + low_byte, R_SH_IMM4, 0);
  828.           break;
  829.         case IMM_8BY4:
  830.           insert (output + low_byte, R_SH_IMM8BY4, 0);
  831.           break;
  832.         case IMM_8BY2:
  833.           insert (output + low_byte, R_SH_IMM8BY2, 0);
  834.           break;
  835.         case IMM_8:
  836.           insert (output + low_byte, R_SH_IMM8, 0);
  837.           break;
  838.         case PCRELIMM_8BY4:
  839.           insert (output, R_SH_PCRELIMM8BY4, 1);
  840.           break;
  841.         case PCRELIMM_8BY2:
  842.           insert (output, R_SH_PCRELIMM8BY2, 1);
  843.           break;
  844.         default:
  845.           printf ("failed for %d\n", i);
  846.         }
  847.     }
  848.     }
  849.   if (! target_big_endian) {
  850.     output[1] = (nbuf[0] << 4) | (nbuf[1]);
  851.     output[0] = (nbuf[2] << 4) | (nbuf[3]);
  852.   }
  853.   else {
  854.     output[0] = (nbuf[0] << 4) | (nbuf[1]);
  855.     output[1] = (nbuf[2] << 4) | (nbuf[3]);
  856.   }
  857. }
  858.  
  859. /* This is the guts of the machine-dependent assembler.  STR points to a
  860.    machine dependent instruction.  This function is supposed to emit
  861.    the frags/bytes it assembles to.
  862.  */
  863.  
  864. void
  865. md_assemble (str)
  866.      char *str;
  867. {
  868.   unsigned char *op_start;
  869.   unsigned char *op_end;
  870.   sh_operand_info operand[3];
  871.   sh_opcode_info *opcode;
  872.   char name[20];
  873.   int nlen = 0;
  874.   /* Drop leading whitespace */
  875.   while (*str == ' ')
  876.     str++;
  877.  
  878.   /* find the op code end */
  879.   for (op_start = op_end = (unsigned char *) (str);
  880.        *op_end
  881.        && nlen < 20
  882.        && !is_end_of_line[*op_end] && *op_end != ' ';
  883.        op_end++)
  884.     {
  885.       name[nlen] = op_start[nlen];
  886.       nlen++;
  887.     }
  888.   name[nlen] = 0;
  889.  
  890.   if (nlen == 0)
  891.     {
  892.       as_bad ("can't find opcode ");
  893.     }
  894.  
  895.   opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
  896.  
  897.   if (opcode == NULL)
  898.     {
  899.       as_bad ("unknown opcode");
  900.       return;
  901.     }
  902.  
  903.   if (sh_relax
  904.       && ! seg_info (now_seg)->tc_segment_info_data.in_code)
  905.     {
  906.       /* Output a CODE reloc to tell the linker that the following
  907.          bytes are instructions, not data.  */
  908.       fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0, R_SH_CODE);
  909.       seg_info (now_seg)->tc_segment_info_data.in_code = 1;
  910.     }
  911.  
  912.   if (opcode->arg[0] == A_BDISP12
  913.       || opcode->arg[0] == A_BDISP8)
  914.     {
  915.       parse_exp (op_end + 1);
  916.       build_relax (opcode);
  917.     }
  918.   else
  919.     {
  920.       if (opcode->arg[0] != A_END)
  921.     {
  922.       get_operands (opcode, op_end, operand);
  923.     }
  924.       opcode = get_specific (opcode, operand);
  925.  
  926.       if (opcode == 0)
  927.     {
  928.       /* Couldn't find an opcode which matched the operands */
  929.       char *where = frag_more (2);
  930.  
  931.       where[0] = 0x0;
  932.       where[1] = 0x0;
  933.       as_bad ("invalid operands for opcode");
  934.       return;
  935.     }
  936.  
  937.       build_Mytes (opcode, operand);
  938.     }
  939.  
  940. }
  941.  
  942. /* This routine is called each time a label definition is seen.  It
  943.    emits a R_SH_LABEL reloc if necessary.  */
  944.  
  945. void
  946. sh_frob_label ()
  947. {
  948.   static fragS *last_label_frag;
  949.   static int last_label_offset;
  950.  
  951.   if (sh_relax
  952.       && seg_info (now_seg)->tc_segment_info_data.in_code)
  953.     {
  954.       int offset;
  955.  
  956.       offset = frag_now_fix ();
  957.       if (frag_now != last_label_frag
  958.       || offset != last_label_offset)
  959.     {    
  960.       fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, R_SH_LABEL);
  961.       last_label_frag = frag_now;
  962.       last_label_offset = offset;
  963.     }
  964.     }
  965. }
  966.  
  967. /* This routine is called when the assembler is about to output some
  968.    data.  It emits a R_SH_DATA reloc if necessary.  */
  969.  
  970. void
  971. sh_flush_pending_output ()
  972. {
  973.   if (sh_relax
  974.       && seg_info (now_seg)->tc_segment_info_data.in_code)
  975.     {
  976.       fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0, R_SH_DATA);
  977.       seg_info (now_seg)->tc_segment_info_data.in_code = 0;
  978.     }
  979. }
  980.  
  981. void
  982. DEFUN (tc_crawl_symbol_chain, (headers),
  983.        object_headers * headers)
  984. {
  985.   printf ("call to tc_crawl_symbol_chain \n");
  986. }
  987.  
  988. symbolS *
  989. DEFUN (md_undefined_symbol, (name),
  990.        char *name)
  991. {
  992.   return 0;
  993. }
  994.  
  995. void
  996. DEFUN (tc_headers_hook, (headers),
  997.        object_headers * headers)
  998. {
  999.   printf ("call to tc_headers_hook \n");
  1000. }
  1001.  
  1002. /* Various routines to kill one day */
  1003. /* Equal to MAX_PRECISION in atof-ieee.c */
  1004. #define MAX_LITTLENUMS 6
  1005.  
  1006. /* Turn a string in input_line_pointer into a floating point constant of type
  1007.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  1008.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  1009.  */
  1010. char *
  1011. md_atof (type, litP, sizeP)
  1012.      int type;
  1013.      char *litP;
  1014.      int *sizeP;
  1015. {
  1016.   int prec;
  1017.   LITTLENUM_TYPE words[4];
  1018.   char *t;
  1019.   int i;
  1020.  
  1021.   switch (type)
  1022.     {
  1023.     case 'f':
  1024.       prec = 2;
  1025.       break;
  1026.  
  1027.     case 'd':
  1028.       prec = 4;
  1029.       break;
  1030.  
  1031.     default:
  1032.       *sizeP = 0;
  1033.       return "bad call to md_atof";
  1034.     }
  1035.  
  1036.   t = atof_ieee (input_line_pointer, type, words);
  1037.   if (t)
  1038.     input_line_pointer = t;
  1039.  
  1040.   *sizeP = prec * 2;
  1041.  
  1042.   if (! target_big_endian)
  1043.     {
  1044.       for (i = prec - 1; i >= 0; i--)
  1045.     {
  1046.       md_number_to_chars (litP, (valueT) words[i], 2);
  1047.       litP += 2;
  1048.     }
  1049.     }
  1050.   else
  1051.     {
  1052.       for (i = 0; i < prec; i++)
  1053.     {
  1054.       md_number_to_chars (litP, (valueT) words[i], 2);
  1055.       litP += 2;
  1056.     }
  1057.     }
  1058.      
  1059.   return NULL;
  1060. }
  1061.  
  1062. /* Handle the .uses pseudo-op.  This pseudo-op is used just before a
  1063.    call instruction.  It refers to a label of the instruction which
  1064.    loads the register which the call uses.  We use it to generate a
  1065.    special reloc for the linker.  */
  1066.  
  1067. static void
  1068. s_uses (ignore)
  1069.      int ignore;
  1070. {
  1071.   expressionS ex;
  1072.  
  1073.   if (! sh_relax)
  1074.     as_warn (".uses pseudo-op seen when not relaxing");
  1075.  
  1076.   expression (&ex);
  1077.  
  1078.   if (ex.X_op != O_symbol || ex.X_add_number != 0)
  1079.     {
  1080.       as_bad ("bad .uses format");
  1081.       ignore_rest_of_line ();
  1082.       return;
  1083.     }
  1084.  
  1085.   fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, R_SH_USES);
  1086.  
  1087.   demand_empty_rest_of_line ();
  1088. }
  1089.  
  1090. CONST char *md_shortopts = "";
  1091. struct option md_longopts[] = {
  1092.  
  1093. #define OPTION_RELAX  (OPTION_MD_BASE)
  1094. #define OPTION_LITTLE (OPTION_MD_BASE+1)
  1095.  
  1096.   {"relax", no_argument, NULL, OPTION_RELAX},
  1097.   {"little", no_argument, NULL, OPTION_LITTLE},
  1098.   {NULL, no_argument, NULL, 0}
  1099. };
  1100. size_t md_longopts_size = sizeof(md_longopts);
  1101.  
  1102. int
  1103. md_parse_option (c, arg)
  1104.      int c;
  1105.      char *arg;
  1106. {
  1107.   switch (c)
  1108.     {
  1109.     case OPTION_RELAX:
  1110.       sh_relax = 1;
  1111.       break;
  1112.     case OPTION_LITTLE:
  1113.       shl = 1;
  1114.       target_big_endian = 0;
  1115.       break;
  1116.  
  1117.     default:
  1118.       return 0;
  1119.     }
  1120.  
  1121.   return 1;
  1122. }
  1123.  
  1124. void
  1125. md_show_usage (stream)
  1126.      FILE *stream;
  1127. {
  1128.   fprintf(stream, "\
  1129. SH options:\n\
  1130. -little            generate little endian code\n\
  1131. -relax            alter jump instructions for long displacements\n");
  1132. }
  1133.  
  1134. int md_short_jump_size;
  1135.  
  1136. void
  1137. tc_Nout_fix_to_chars ()
  1138. {
  1139.   printf ("call to tc_Nout_fix_to_chars \n");
  1140.   abort ();
  1141. }
  1142.  
  1143. void
  1144. md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
  1145.      char *ptr;
  1146.      addressT from_Nddr;
  1147.      addressT to_Nddr;
  1148.      fragS *frag;
  1149.      symbolS *to_symbol;
  1150. {
  1151.   as_fatal ("failed sanity check.");
  1152. }
  1153.  
  1154. void
  1155. md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
  1156.      char *ptr;
  1157.      addressT from_Nddr, to_Nddr;
  1158.      fragS *frag;
  1159.      symbolS *to_symbol;
  1160. {
  1161.   as_fatal ("failed sanity check.");
  1162. }
  1163.  
  1164. /* This is function is called after the symbol table has been
  1165.    completed, but before md_convert_frag has been called.  If we have
  1166.    seen any .uses pseudo-ops, they point to an instruction which loads
  1167.    a register with the address of a function.  We look through the
  1168.    fixups to find where the function address is being loaded from.  We
  1169.    then generate a COUNT reloc giving the number of times that
  1170.    function address is referred to.  The linker uses this information
  1171.    when doing relaxing, to decide when it can eliminate the stored
  1172.    function address entirely.  */
  1173.  
  1174. void
  1175. sh_coff_frob_file ()
  1176. {
  1177.   int iseg;
  1178.  
  1179.   if (! sh_relax)
  1180.     return;
  1181.  
  1182.   for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
  1183.     {
  1184.       fixS *fix;
  1185.  
  1186.       for (fix = segment_info[iseg].fix_root; fix != NULL; fix = fix->fx_next)
  1187.     {
  1188.       symbolS *sym;
  1189.       bfd_vma val;
  1190.       fixS *fscan;
  1191.       int iscan;
  1192.       int count;
  1193.  
  1194.       if (fix->fx_r_type != R_SH_USES)
  1195.         continue;
  1196.  
  1197.       /* The R_SH_USES reloc should refer to a defined local
  1198.              symbol in the same section.  */
  1199.       sym = fix->fx_addsy;
  1200.       if (sym == NULL
  1201.           || fix->fx_subsy != NULL
  1202.           || fix->fx_addnumber != 0
  1203.           || S_GET_SEGMENT (sym) != iseg
  1204.           || S_GET_STORAGE_CLASS (sym) == C_EXT)
  1205.         {
  1206.           as_warn_where (fix->fx_file, fix->fx_line,
  1207.                  ".uses does not refer to a local symbol in the same section");
  1208.           continue;
  1209.         }
  1210.  
  1211.       /* Look through the fixups again, this time looking for one
  1212.              at the same location as sym.  */
  1213.       val = S_GET_VALUE (sym);
  1214.       for (fscan = segment_info[iseg].fix_root;
  1215.            fscan != NULL;
  1216.            fscan = fscan->fx_next)
  1217.         if (val == fscan->fx_frag->fr_address + fscan->fx_where
  1218.         && fscan->fx_r_type != R_SH_ALIGN
  1219.         && fscan->fx_r_type != R_SH_CODE
  1220.         && fscan->fx_r_type != R_SH_DATA
  1221.         && fscan->fx_r_type != R_SH_LABEL)
  1222.           break;
  1223.       if (fscan == NULL)
  1224.         {
  1225.           as_warn_where (fix->fx_file, fix->fx_line,
  1226.                  "can't find fixup pointed to by .uses");
  1227.           continue;
  1228.         }
  1229.  
  1230.       if (fscan->fx_tcbit)
  1231.         {
  1232.           /* We've already done this one.  */
  1233.           continue;
  1234.         }
  1235.  
  1236.       /* fscan should also be a fixup to a local symbol in the same
  1237.              section.  */
  1238.       sym = fscan->fx_addsy;
  1239.       if (sym == NULL
  1240.           || fscan->fx_subsy != NULL
  1241.           || fscan->fx_addnumber != 0
  1242.           || S_GET_SEGMENT (sym) != iseg
  1243.           || S_GET_STORAGE_CLASS (sym) == C_EXT)
  1244.         {
  1245.           as_warn_where (fix->fx_file, fix->fx_line,
  1246.                  ".uses target does not refer to a local symbol in the same section");
  1247.           continue;
  1248.         }
  1249.  
  1250.       /* Now we look through all the fixups of all the sections,
  1251.          counting the number of times we find a reference to sym.  */
  1252.       count = 0;
  1253.       for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
  1254.         {
  1255.           for (fscan = segment_info[iscan].fix_root;
  1256.            fscan != NULL;
  1257.            fscan = fscan->fx_next)
  1258.         {
  1259.           if (fscan->fx_addsy == sym)
  1260.             {
  1261.               ++count;
  1262.               fscan->fx_tcbit = 1;
  1263.             }
  1264.         }
  1265.         }
  1266.  
  1267.       if (count < 1)
  1268.         abort ();
  1269.  
  1270.       /* Generate a R_SH_COUNT fixup at the location of sym.  We
  1271.              have already adjusted the value of sym to include the
  1272.              fragment address, so we undo that adjustment here.  */
  1273.       subseg_change (iseg, 0);
  1274.       fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
  1275.            4, &abs_symbol, count, 0, R_SH_COUNT);
  1276.     }
  1277.     }
  1278. }
  1279.  
  1280. /* Called after relaxing.  Set the correct sizes of the fragments, and
  1281.    create relocs so that md_apply_fix will fill in the correct values.  */
  1282.  
  1283. void
  1284. md_convert_frag (headers, seg, fragP)
  1285.      object_headers *headers;
  1286.      segT seg;
  1287.      fragS *fragP;
  1288. {
  1289.   int donerelax = 0;
  1290.  
  1291.   switch (fragP->fr_subtype)
  1292.     {
  1293.     case C (COND_JUMP, COND8):
  1294.       subseg_change (seg, 0);
  1295.       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
  1296.            1, R_SH_PCDISP8BY2);
  1297.       fragP->fr_fix += 2;
  1298.       fragP->fr_var = 0;
  1299.       break;
  1300.  
  1301.     case C (UNCOND_JUMP, UNCOND12):
  1302.       subseg_change (seg, 0);
  1303.       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
  1304.            1, R_SH_PCDISP);
  1305.       fragP->fr_fix += 2;
  1306.       fragP->fr_var = 0;
  1307.       break;
  1308.  
  1309.     case C (UNCOND_JUMP, UNCOND32):
  1310.     case C (UNCOND_JUMP, UNDEF_WORD_DISP):
  1311.       if (fragP->fr_symbol == NULL)
  1312.     as_bad ("at 0x%lx, displacement overflows 12-bit field",
  1313.         (unsigned long) fragP->fr_address);
  1314.       else
  1315.     as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
  1316.         (unsigned long) fragP->fr_address,        
  1317.         S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
  1318.         S_GET_NAME (fragP->fr_symbol));
  1319.  
  1320. #if 0                /* This code works, but generates poor code and the compiler
  1321.                    should never produce a sequence that requires it to be used.  */
  1322.  
  1323.       /* A jump wont fit in 12 bits, make code which looks like
  1324.      bra foo
  1325.      mov.w @(0, PC), r14
  1326.      .long disp
  1327.      foo: bra @r14
  1328.      */
  1329.       int t = buffer[0] & 0x10;
  1330.  
  1331.       buffer[highbyte] = 0xa0;    /* branch over move and disp */
  1332.       buffer[lowbyte] = 3;
  1333.       buffer[highbyte+2] = 0xd0 | JREG;    /* Build mov insn */
  1334.       buffer[lowbyte+2] = 0x00;
  1335.  
  1336.       buffer[highbyte+4] = 0;    /* space for 32 bit jump disp */
  1337.       buffer[lowbyte+4] = 0;
  1338.       buffer[highbyte+6] = 0;
  1339.       buffer[lowbyte+6] = 0;
  1340.  
  1341.       buffer[highbyte+8] = 0x40 | JREG;    /* Build jmp @JREG */
  1342.       buffer[lowbyte+8] = t ? 0xb : 0x2b;
  1343.  
  1344.       buffer[highbyte+10] = 0x20; /* build nop */
  1345.       buffer[lowbyte+10] = 0x0b;
  1346.  
  1347.       /* Make reloc for the long disp */
  1348.       fix_new (fragP,
  1349.            fragP->fr_fix + 4,
  1350.            4,
  1351.            fragP->fr_symbol,
  1352.            fragP->fr_offset,
  1353.            0,
  1354.            R_SH_IMM32);
  1355.       fragP->fr_fix += UNCOND32_LENGTH;
  1356.       fragP->fr_var = 0;
  1357.       donerelax = 1;
  1358. #endif
  1359.  
  1360.       break;
  1361.  
  1362.     case C (COND_JUMP, COND12):
  1363.       /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
  1364.       {
  1365.     unsigned char *buffer =
  1366.       (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
  1367.     int highbyte = target_big_endian ? 0 : 1;
  1368.     int lowbyte = target_big_endian ? 1 : 0;
  1369.  
  1370.     /* Toggle the true/false bit of the bcond.  */
  1371.     buffer[highbyte] ^= 0x2;
  1372.  
  1373.     /* Build a relocation to six bytes farther on.  */
  1374.     subseg_change (seg, 0);
  1375.     fix_new (fragP, fragP->fr_fix, 2,
  1376.          segment_info[seg].dot,
  1377.          fragP->fr_address + fragP->fr_fix + 6,
  1378.          1, R_SH_PCDISP8BY2);
  1379.  
  1380.     /* Set up a jump instruction.  */
  1381.     buffer[highbyte + 2] = 0xa0;
  1382.     buffer[lowbyte + 2] = 0;
  1383.     fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
  1384.          fragP->fr_offset, 1, R_SH_PCDISP);
  1385.  
  1386.     /* Fill in a NOP instruction.  */
  1387.     buffer[highbyte + 4] = 0x0;
  1388.     buffer[lowbyte + 4] = 0x9;
  1389.  
  1390.     fragP->fr_fix += 6;
  1391.     fragP->fr_var = 0;
  1392.     donerelax = 1;
  1393.       }
  1394.       break;
  1395.  
  1396.     case C (COND_JUMP, COND32):
  1397.     case C (COND_JUMP, UNDEF_WORD_DISP):
  1398.       if (fragP->fr_symbol == NULL)
  1399.     as_bad ("at 0x%lx, displacement overflows 8-bit field", 
  1400.         (unsigned long) fragP->fr_address);
  1401.       else  
  1402.     as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
  1403.         (unsigned long) fragP->fr_address,        
  1404.         S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
  1405.         S_GET_NAME (fragP->fr_symbol));
  1406.  
  1407. #if 0                /* This code works, but generates poor code, and the compiler
  1408.                    should never produce a sequence that requires it to be used.  */
  1409.  
  1410.       /* A bcond won't fit and it won't go into a 12 bit
  1411.      displacement either, the code sequence looks like:
  1412.      b!cond foop
  1413.      mov.w @(n, PC), r14
  1414.      jmp  @r14
  1415.      nop
  1416.      .long where
  1417.      foop:
  1418.      */
  1419.  
  1420.       buffer[0] ^= 0x2;        /* Toggle T/F bit */
  1421. #define JREG 14
  1422.       buffer[1] = 5;        /* branch over mov, jump, nop and ptr */
  1423.       buffer[2] = 0xd0 | JREG;    /* Build mov insn */
  1424.       buffer[3] = 0x2;
  1425.       buffer[4] = 0x40 | JREG;    /* Build jmp @JREG */
  1426.       buffer[5] = 0x0b;
  1427.       buffer[6] = 0x20;        /* build nop */
  1428.       buffer[7] = 0x0b;
  1429.       buffer[8] = 0;        /* space for 32 bit jump disp */
  1430.       buffer[9] = 0;
  1431.       buffer[10] = 0;
  1432.       buffer[11] = 0;
  1433.       buffer[12] = 0;
  1434.       buffer[13] = 0;
  1435.       /* Make reloc for the long disp */
  1436.       fix_new (fragP,
  1437.            fragP->fr_fix + 8,
  1438.            4,
  1439.            fragP->fr_symbol,
  1440.            fragP->fr_offset,
  1441.            0,
  1442.            R_SH_IMM32);
  1443.       fragP->fr_fix += COND32_LENGTH;
  1444.       fragP->fr_var = 0;
  1445.       donerelax = 1;
  1446. #endif
  1447.  
  1448.       break;
  1449.  
  1450.     default:
  1451.       abort ();
  1452.     }
  1453.  
  1454.   if (donerelax && !sh_relax)
  1455.     as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
  1456.          (unsigned long) fragP->fr_address,
  1457.          fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
  1458.          (unsigned long) fragP->fr_offset);
  1459. }
  1460.  
  1461. valueT
  1462. DEFUN (md_section_align, (seg, size),
  1463.        segT seg AND
  1464.        valueT size)
  1465. {
  1466.   return ((size + (1 << section_alignment[(int) seg]) - 1)
  1467.       & (-1 << section_alignment[(int) seg]));
  1468.  
  1469. }
  1470.  
  1471. /* When relaxing, we need to output a reloc for any .align directive
  1472.    that requests alignment to a four byte boundary or larger.  */
  1473.  
  1474. void
  1475. sh_handle_align (frag)
  1476.      fragS *frag;
  1477. {
  1478.   if (sh_relax
  1479.       && frag->fr_type == rs_align
  1480.       && frag->fr_address + frag->fr_fix > 0
  1481.       && frag->fr_offset > 1
  1482.       && now_seg != bss_section)
  1483.     fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
  1484.          R_SH_ALIGN);
  1485. }
  1486.  
  1487. /* This macro decides whether a particular reloc is an entry in a
  1488.    switch table.  It is used when relaxing, because the linker needs
  1489.    to know about all such entries so that it can adjust them if
  1490.    necessary.  */
  1491.  
  1492. #define SWITCH_TABLE(fix)                \
  1493.   ((fix)->fx_addsy != NULL                \
  1494.    && (fix)->fx_subsy != NULL                \
  1495.    && S_GET_SEGMENT ((fix)->fx_addsy) == text_section    \
  1496.    && S_GET_SEGMENT ((fix)->fx_subsy) == text_section    \
  1497.    && ((fix)->fx_r_type == R_SH_IMM32            \
  1498.        || (fix)->fx_r_type == R_SH_IMM16        \
  1499.        || ((fix)->fx_r_type == 0            \
  1500.        && ((fix)->fx_size == 2            \
  1501.            || (fix)->fx_size == 4))))
  1502.  
  1503. /* See whether we need to force a relocation into the output file.
  1504.    This is used to force out switch and PC relative relocations when
  1505.    relaxing.  */
  1506.  
  1507. int
  1508. sh_force_relocation (fix)
  1509.      fixS *fix;
  1510. {
  1511.   if (! sh_relax)
  1512.     return 0;
  1513.  
  1514.   return (fix->fx_pcrel
  1515.       || SWITCH_TABLE (fix)
  1516.       || fix->fx_r_type == R_SH_COUNT
  1517.       || fix->fx_r_type == R_SH_ALIGN
  1518.       || fix->fx_r_type == R_SH_CODE
  1519.       || fix->fx_r_type == R_SH_DATA
  1520.       || fix->fx_r_type == R_SH_LABEL);
  1521. }
  1522.  
  1523. /* Apply a fixup to the object file.  */
  1524.  
  1525. void
  1526. md_apply_fix (fixP, val)
  1527.      fixS *fixP;
  1528.      long val;
  1529. {
  1530.   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
  1531.   int lowbyte = target_big_endian ? 1 : 0;
  1532.   int highbyte = target_big_endian ? 0 : 1;
  1533.  
  1534.   if (fixP->fx_r_type == 0)
  1535.     {
  1536.       if (fixP->fx_size == 2)
  1537.     fixP->fx_r_type = R_SH_IMM16;
  1538.       else if (fixP->fx_size == 4)
  1539.     fixP->fx_r_type = R_SH_IMM32;
  1540.       else if (fixP->fx_size == 1)
  1541.     fixP->fx_r_type = R_SH_IMM8;
  1542.       else
  1543.     abort ();
  1544.     }
  1545.  
  1546.   switch (fixP->fx_r_type)
  1547.     {
  1548.     case R_SH_IMM4:
  1549.       *buf = (*buf & 0xf0) | (val & 0xf);
  1550.       break;
  1551.  
  1552.     case R_SH_IMM4BY2:
  1553.       *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
  1554.       break;
  1555.  
  1556.     case R_SH_IMM4BY4:
  1557.       *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
  1558.       break;
  1559.  
  1560.     case R_SH_IMM8BY2:
  1561.       *buf = val >> 1;
  1562.       break;
  1563.  
  1564.     case R_SH_IMM8BY4:
  1565.       *buf = val >> 2;
  1566.       break;
  1567.  
  1568.     case R_SH_IMM8:
  1569.       *buf++ = val;
  1570.       break;
  1571.  
  1572.     case R_SH_PCRELIMM8BY4:
  1573.       /* The lower two bits of the PC are cleared before the
  1574.          displacement is added in.  We can assume that the destination
  1575.          is on a 4 byte bounday.  If this instruction is also on a 4
  1576.          byte boundary, then we want
  1577.        (target - here) / 4
  1578.      and target - here is a multiple of 4.
  1579.      Otherwise, we are on a 2 byte boundary, and we want
  1580.        (target - (here - 2)) / 4
  1581.      and target - here is not a multiple of 4.  Computing
  1582.        (target - (here - 2)) / 4 == (target - here + 2) / 4
  1583.      works for both cases, since in the first case the addition of
  1584.      2 will be removed by the division.  target - here is in the
  1585.      variable val.  */
  1586.       val = (val + 2) / 4;
  1587.       if (val & ~0xff)
  1588.     as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
  1589.       buf[lowbyte] = val;
  1590.       break;
  1591.  
  1592.     case R_SH_PCRELIMM8BY2:
  1593.       val /= 2;
  1594.       if (val & ~0xff)
  1595.     as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
  1596.       buf[lowbyte] = val;
  1597.       break;
  1598.  
  1599.     case R_SH_PCDISP8BY2:
  1600.       val /= 2;
  1601.       if (val < -0x80 || val > 0x7f)
  1602.     as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
  1603.       buf[lowbyte] = val;
  1604.       break;
  1605.  
  1606.     case R_SH_PCDISP:
  1607.       val /= 2;
  1608.       if (val < -0x800 || val >= 0x7ff)
  1609.     as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
  1610.       buf[lowbyte] = val & 0xff;
  1611.       buf[highbyte] |= (val >> 8) & 0xf;
  1612.       break;
  1613.  
  1614.     case R_SH_IMM32:
  1615.       if (! target_big_endian) 
  1616.     {
  1617.       *buf++ = val >> 0;
  1618.       *buf++ = val >> 8;
  1619.       *buf++ = val >> 16;
  1620.       *buf++ = val >> 24;
  1621.     }
  1622.       else 
  1623.     {
  1624.       *buf++ = val >> 24;
  1625.       *buf++ = val >> 16;
  1626.       *buf++ = val >> 8;
  1627.       *buf++ = val >> 0;
  1628.     }
  1629.       break;
  1630.  
  1631.     case R_SH_IMM16:
  1632.       if (! target_big_endian)
  1633.     {
  1634.       *buf++ = val >> 0;
  1635.       *buf++ = val >> 8;
  1636.     } 
  1637.       else 
  1638.     {
  1639.       *buf++ = val >> 8;
  1640.       *buf++ = val >> 0;
  1641.     }
  1642.       break;
  1643.  
  1644.     case R_SH_USES:
  1645.       /* Pass the value into sh_coff_reloc_mangle.  */
  1646.       fixP->fx_addnumber = val;
  1647.       break;
  1648.  
  1649.     case R_SH_COUNT:
  1650.     case R_SH_ALIGN:
  1651.     case R_SH_CODE:
  1652.     case R_SH_DATA:
  1653.     case R_SH_LABEL:
  1654.       /* Nothing to do here.  */
  1655.       break;
  1656.  
  1657.     default:
  1658.       abort ();
  1659.     }
  1660. }
  1661.  
  1662. int md_long_jump_size;
  1663.  
  1664. /* Called just before address relaxation.  Return the length
  1665.    by which a fragment must grow to reach it's destination.  */
  1666.  
  1667. int
  1668. md_estimate_size_before_relax (fragP, segment_type)
  1669.      register fragS *fragP;
  1670.      register segT segment_type;
  1671. {
  1672.   switch (fragP->fr_subtype)
  1673.     {
  1674.     case C (UNCOND_JUMP, UNDEF_DISP):
  1675.       /* used to be a branch to somewhere which was unknown */
  1676.       if (!fragP->fr_symbol)
  1677.     {
  1678.       fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
  1679.       fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
  1680.     }
  1681.       else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
  1682.     {
  1683.       fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
  1684.       fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
  1685.     }
  1686.       else
  1687.     {
  1688.       fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
  1689.       fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
  1690.       return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
  1691.     }
  1692.       break;
  1693.  
  1694.     default:
  1695.       abort ();
  1696.     case C (COND_JUMP, UNDEF_DISP):
  1697.       /* used to be a branch to somewhere which was unknown */
  1698.       if (fragP->fr_symbol
  1699.       && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
  1700.     {
  1701.       /* Got a symbol and it's defined in this segment, become byte
  1702.          sized - maybe it will fix up */
  1703.       fragP->fr_subtype = C (COND_JUMP, COND8);
  1704.       fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
  1705.     }
  1706.       else if (fragP->fr_symbol)
  1707.     {
  1708.       /* Its got a segment, but its not ours, so it will always be long */
  1709.       fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
  1710.       fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
  1711.       return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
  1712.     }
  1713.       else
  1714.     {
  1715.       /* We know the abs value */
  1716.       fragP->fr_subtype = C (COND_JUMP, COND8);
  1717.       fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
  1718.     }
  1719.  
  1720.       break;
  1721.     }
  1722.   return fragP->fr_var;
  1723. }
  1724.  
  1725. /* Put number into target byte order */
  1726.  
  1727. void
  1728. md_number_to_chars (ptr, use, nbytes)
  1729.      char *ptr;
  1730.      valueT use;
  1731.      int nbytes;
  1732. {
  1733.   if (! target_big_endian)
  1734.     number_to_chars_littleendian (ptr, use, nbytes);
  1735.   else
  1736.     number_to_chars_bigendian (ptr, use, nbytes);
  1737. }
  1738.  
  1739. long
  1740. md_pcrel_from (fixP)
  1741.      fixS *fixP;
  1742. {
  1743.   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
  1744. }
  1745.  
  1746. int
  1747. tc_coff_sizemachdep (frag)
  1748.      fragS *frag;
  1749. {
  1750.   return md_relax_table[frag->fr_subtype].rlx_length;
  1751. }
  1752.  
  1753. /* When we align the .text section, insert the correct NOP pattern.  */
  1754.  
  1755. int
  1756. sh_do_align (n, fill, len)
  1757.      int n;
  1758.      const char *fill;
  1759.      int len;
  1760. {
  1761.   if ((fill == NULL || (*fill == 0 && len == 1))
  1762.       && (now_seg == text_section
  1763. #ifdef BFD_ASSEMBLER
  1764.       || (now_seg->flags & SEC_CODE) != 0
  1765. #endif
  1766.       || strcmp (obj_segment_name (now_seg), ".init") == 0)
  1767.       && n > 1)
  1768.     {
  1769.       static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
  1770.       static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
  1771.  
  1772.       /* First align to a 2 byte boundary, in case there is an odd
  1773.          .byte.  */
  1774.       frag_align (1, 0);
  1775.       if (target_big_endian)
  1776.     frag_align_pattern (n, big_nop_pattern, sizeof big_nop_pattern);
  1777.       else
  1778.     frag_align_pattern (n, little_nop_pattern, sizeof little_nop_pattern);
  1779.       return 1;
  1780.     }
  1781.  
  1782.   return 0;
  1783. }
  1784.  
  1785. #ifdef OBJ_COFF
  1786.  
  1787. /* Adjust a reloc for the SH.  This is similar to the generic code,
  1788.    but does some minor tweaking.  */
  1789.  
  1790. void
  1791. sh_coff_reloc_mangle (seg, fix, intr, paddr)
  1792.      segment_info_type *seg;
  1793.      fixS *fix;
  1794.      struct internal_reloc *intr;
  1795.      unsigned int paddr;
  1796. {
  1797.   symbolS *symbol_ptr = fix->fx_addsy;
  1798.   symbolS *dot;
  1799.  
  1800.   intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
  1801.  
  1802.   if (! SWITCH_TABLE (fix))
  1803.     {
  1804.       intr->r_type = fix->fx_r_type;
  1805.       intr->r_offset = 0;
  1806.     }
  1807.   else
  1808.     {
  1809.       know (sh_relax);
  1810.  
  1811.       if (fix->fx_r_type == R_SH_IMM16)
  1812.     intr->r_type = R_SH_SWITCH16;
  1813.       else if (fix->fx_r_type == R_SH_IMM32)
  1814.     intr->r_type = R_SH_SWITCH32;
  1815.       else
  1816.     abort ();
  1817.  
  1818.       /* For a switch reloc, we set r_offset to the difference between
  1819.          the reloc address and the subtrahend.  When the linker is
  1820.          doing relaxing, it can use the determine the starting and
  1821.          ending points of the switch difference expression.  */
  1822.       intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
  1823.     }
  1824.  
  1825.   /* PC relative relocs are always against the current section.  */
  1826.   if (symbol_ptr == NULL)
  1827.     {
  1828.       switch (fix->fx_r_type)
  1829.     {
  1830.     case R_SH_PCRELIMM8BY2:
  1831.     case R_SH_PCRELIMM8BY4:
  1832.     case R_SH_PCDISP8BY2:
  1833.     case R_SH_PCDISP:
  1834.     case R_SH_USES:
  1835.       symbol_ptr = seg->dot;
  1836.       break;
  1837.     default:
  1838.       break;
  1839.     }
  1840.     }
  1841.  
  1842.   if (fix->fx_r_type == R_SH_USES)
  1843.     {
  1844.       /* We can't store the offset in the object file, since this
  1845.      reloc does not take up any space, so we store it in r_offset.
  1846.      The fx_addnumber field was set in md_apply_fix.  */
  1847.       intr->r_offset = fix->fx_addnumber;
  1848.     }
  1849.   else if (fix->fx_r_type == R_SH_COUNT)
  1850.     {
  1851.       /* We can't store the count in the object file, since this reloc
  1852.          does not take up any space, so we store it in r_offset.  The
  1853.          fx_offset field was set when the fixup was created in
  1854.          sh_coff_frob_file.  */
  1855.       intr->r_offset = fix->fx_offset;
  1856.       /* This reloc is always absolute.  */
  1857.       symbol_ptr = NULL;
  1858.     }
  1859.   else if (fix->fx_r_type == R_SH_ALIGN)
  1860.     {
  1861.       /* Store the alignment in the r_offset field.  */
  1862.       intr->r_offset = fix->fx_offset;
  1863.       /* This reloc is always absolute.  */
  1864.       symbol_ptr = NULL;
  1865.     }
  1866.   else if (fix->fx_r_type == R_SH_CODE
  1867.        || fix->fx_r_type == R_SH_DATA
  1868.        || fix->fx_r_type == R_SH_LABEL)
  1869.     {
  1870.       /* These relocs are always absolute.  */
  1871.       symbol_ptr = NULL;
  1872.     }
  1873.  
  1874.   /* Turn the segment of the symbol into an offset.  */
  1875.   if (symbol_ptr != NULL)
  1876.     {
  1877.       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
  1878.       if (dot != NULL)
  1879.     intr->r_symndx = dot->sy_number;
  1880.       else
  1881.     intr->r_symndx = symbol_ptr->sy_number;
  1882.     }
  1883.   else
  1884.     intr->r_symndx = -1;
  1885. }
  1886.  
  1887. #endif
  1888.